package com.company.cloud.mall.modules.task.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.utils.LocalDateTimeUtils;
import com.company.cloud.mall.modules.api.constant.OrderStateEnum;
import com.company.cloud.mall.modules.app.service.ShopOrderPayService;
import com.company.cloud.mall.modules.app.service.ShopOrderService;
import com.company.cloud.mall.modules.base.entity.OrderInfo;
import com.company.cloud.mall.modules.base.service.OrderInfoService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Log4j2
@Component
public class ShopOrderTaskService {

	@Autowired
	private ShopOrderService shopOrderService;

	@Autowired
	private OrderInfoService orderInfoService;

	@Autowired
	private ShopOrderPayService shopOrderPayService;

	/**
	 * 支付结果查询任务
	 */
	@Scheduled(cron = "0 0/3 * * * ?")
	// TODO 将 Xxl
//	@XxlJob("Mall_OrderPayResult_JobHandler")
	public String payResultQueryTask(String param) throws Exception {
		String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//		// XxlJobLogger.log("+++++支付结果查询任务[开始],ShopOrderTaskService.payResultQueryTask,date={}",nowDate);
		log.debug("+++++支付结果查询任务[开始],ShopOrderTaskService.payResultQueryTask,date={}",nowDate);
		LoginUserInfo userInfo = this.getLoginUserInfo();
		shopOrderPayService.payResultTask(null, userInfo);
		// XxlJobLogger.log("+++++支付结果查询任务[结束],ShopOrderTaskService.payResultQueryTask,完成");
		return "SUCCESS";
	}

	/**
	 * 订单关闭任务
	 */
	@Scheduled(cron = "0 0/30 * * * ?")
//	@XxlJob("Mall_OrderClose_JobHandler")
	public String orderExpireTask(String param) throws Exception {
		String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		// XxlJobLogger.log("+++++订单关闭任务[开始],ShopOrderTaskService.orderExpireTask,date={}",nowDate);
		log.debug("+++++订单关闭任务[开始],ShopOrderTaskService.orderExpireTask,date={}",nowDate);

		List<OrderInfo> orderList = orderInfoService.list(new QueryWrapper<OrderInfo>()
				.eq("status", OrderStateEnum.OSE_Pay.getCode()).lt("order_expire", nowDate).last("LIMIT 0,50"));

		if (CollectionUtils.isEmpty(orderList)) {
			// XxlJobLogger.log("+++++订单关闭任务[结束],ShopOrderTaskService.orderExpireTask,无数据");
			return "SUCCESS";
		}

		LoginUserInfo userInfo = this.getLoginUserInfo();
		for (OrderInfo order : orderList) {
			shopOrderService.close(order.getSn(), userInfo);
		}
		// XxlJobLogger.log("+++++订单关闭任务[结束],ShopOrderTaskService.orderExpireTask,完成");
		return "SUCCESS";

	}
	
	/**
	 * 订单完成任务
	 * */
	@Scheduled(cron = "0 0/45 * * * ?")
//	@XxlJob("Mall_OrderComplete_JobHandler")
	public  String orderCompleteTask(String param) throws Exception{
		String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		// XxlJobLogger.log("+++++订单完成任务[开始],ShopOrderTaskService.orderCompleteTask,date={}",nowDate);
		log.debug("+++++订单完成任务[开始],ShopOrderTaskService.orderCompleteTask,date={}",nowDate);
		
		LocalDateTime sendDateTime= LocalDateTimeUtils.minusDay(LocalDateTime.now(), 15);
		List<OrderInfo> orders=orderInfoService.list(new QueryWrapper<OrderInfo>()
				.eq("status", OrderStateEnum.OSE_Receiving.getCode())
				.le("shipping_date", sendDateTime)
				.last("limit 1000")
				);
		if(CollectionUtils.isEmpty(orders)) {
			// XxlJobLogger.log("+++++订单完成任务[结束],ShopOrderTaskService.orderCompleteTask, - 无数据");
			return "SUCCESS";
		}
		LoginUserInfo userInfo = this.getLoginUserInfo();
		for (OrderInfo order : orders) {
			shopOrderService.completeByTask(order, userInfo);
		}
		// XxlJobLogger.log("+++++订单完成任务[结束],ShopOrderTaskService.orderCompleteTask, 执行完成");
		return "SUCCESS";
	}

	private LoginUserInfo getLoginUserInfo() {
		LoginUserInfo userInfo = new LoginUserInfo();
		userInfo.setId(-10);
		userInfo.setUserName("SYS-Task");
		userInfo.setIp("127.0.0.1");
		return userInfo;
	}

}
